/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.common.controller;

import com.google.gson.Gson;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.bean.UserBean;
import com.koron.workflow.common.service.LoginService;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.RedisUtils;
import com.koron.workflow.common.util.WorkflowUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Api(value = "loginController", description = "登录")
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation("登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码（MD5）", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/login.htm")
    public MessageBean login(@ApiIgnore @RequestBody UserBean user, HttpServletResponse response) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        // 登录验证
        StaffBean staff = loginService.getStaffMsg(user);
        if(staff == null) {
        	 Cookie cookie = new Cookie("token", "");
             response.addCookie(cookie);
             msg.setCode(10001);
             msg.setDescription("用户名或密码错误");
             return msg;
        }
        String token = WorkflowUtil.setTokenCookie(response, staff);
        msg.setData(token);
        return msg;
    }

    @ApiOperation("退出登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
    })
    @PostMapping("/logout.htm")
    public MessageBean logout(String token) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        RedisUtils.delete(token);
        if (RedisUtils.get(token) != null) {
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
        }
        return msg;
    }

    @ApiOperation("获取当前登录人员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "query"),
    })
    @GetMapping("/getUserMsg.htm")
    public MessageBean getUserMsg(String token) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), String.class);
        Object val = RedisUtils.get(token);
        if (val == null){
            msg.setCode(10000);
            msg.setDescription("没有获取到登录信息");
            return msg;
        }
        msg.setData(new Gson().fromJson(val.toString(), StaffBean.class));
        return msg;
    }

}
